package com.aipolice.service;

import com.aipolice.dao.UserDAO;
import com.aipolice.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    @Autowired
    UserDAO userDAO;
    public List<User> list() {
        return userDAO.findAll();
    }

    public void delete(int id){
//        真删除
//        userDAO.deleteById(id);
//        软删除
        User user = userDAO.findById(id).get();
        user.setDel(1);
        userDAO.save(user);

    }
    public User findByUsernameAndPassword(String username,String password){
        return userDAO.findUserByUsernameAndPassword(username,password).get();
    }



    public void add(User user){
        userDAO.save(user);
    }

    public User find(int id){
        return userDAO.findById(id).get();
    }

    public User findByPhone(String phone){
        Optional<User> optionalUser = userDAO.findUserByTel(phone);
        if(!optionalUser.isPresent()){
            System.out.println("没有找到用户");
            return null;
        }else{
            return optionalUser.get();
        }
    }

}
